// import { prefix_api } from '@/hooks/useConfig.js'


const request = (options = {}) =>{
	const prefix_api = import.meta.env.VITE_APP_API_URL
	const token = uni.getStorageSync('token');

	return new Promise((resolve, reject) => {
		console.log(prefix_api, "prefix_api")
		console.log(token, "token")
		uni.request({
			url: prefix_api + options.url  || "",
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				'content-type': 'application/json',
				"token": token
			},
			success: res => {
                if (res.data.code === 1) {
                    resolve(res?.data)
                } else {
                    uni.showToast({
                        icon: 'none',
                        title: res.data.msg,
                        duration: 2000
                    })
                }
			},
		})
		// .then(data => {
		// 	let [err, res] = data
		// 	return resolve(res.data)
		// }).catch(err => {
		// 	return reject(err)
		// })
	})
}

const get = (url, data, options = {}) => {
	options.method= 'GET';
	options.data = data;
	options.url = url;
	return request(options)
}

const post = (url, data, options = {}) => {
	options.method= 'POST';
	options.data = data;
	options.url = url;
	return request(options)
}

export default {
	request,
	get,
	post
}

